
//#include "protocol/FFMsgPro.h"
#ifndef COMMENGINE_H
#define COMMENGINE_H
/*------------------------------------------------------*/
#define MAX_NAMEBUF		50
#define MAX_SOCKBUF		65536
#define MAX_UDPBUF		16384
/*------------------------------------------------------*/
#include "config.h"
#include "FFProtocol.h"


#ifdef WINDOWS
#include <winsock2.h>
#include <process.h>
#else 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "wintypes.h"
#include <unistd.h>
#endif

struct FFAddress
{
	struct sockaddr_in host;
};

struct cfgData
{
	int		version;
	long	packetno;
 	char    username[MAX_NAMEBUF];
	char    hostname[MAX_NAMEBUF];
};

struct threadArg
{
	SOCKET udp_sock;
	Protocol *pro;
};

class CommEngine
{
public:
	int  CommInitSock();
	int  CommBindTCP(struct FFAddress addr, unsigned short portno);
	int  CommBindUDP( struct FFAddress addr, unsigned short portno);
	int  CommBroadUDPSend( int portno, const char *buf);
	int  CommUDPSend( unsigned long hostaddr, int portno, const char *buf);
	int  CommListen();
	int CommAccept(struct sockaddr_in &tcpaddr);
	int CommConnect(unsigned long faraddr,int port);
	void CommCloseTCPSock();
	void CommCloseUDPSock();
	void CommCleanUpSock();
	int  CommUdpSelect(fd_set *rdfds,timeval *timeout);
#ifdef WINDOWS
	void RegisterRecvThread(void (*P)(LPVOID),void *);
#else
	void RegisterRecvThread(void* (*P)(LPVOID),void *);
#endif
	void CommStartThread();
	void CommStartSock();
	bool CommUdpRecv(struct RecvBuf *buf);

	struct cfgData *m_cfg;
	SOCKET m_tcpsock;
	SOCKET m_udpsock;
#ifdef WINDOWS
	void (*recvthread)(void *);
#else
	void* (*recvthread)(void *);
#endif
	Protocol *arg; 
};
#endif
